<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" width="85%" destroy-on-close @closed="$emit('closed')" align-center>
        <el-container>
            <el-aside width="200px" v-loading="showGrouploading">
                <el-container>
                    <el-header>
                        <el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
                    </el-header>
                    <el-main class="nopadding">
                        <el-tree ref="group" class="menu" node-key="id" :data="group" :props="groupProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick"></el-tree>
                    </el-main>
                </el-container>
            </el-aside>
            <el-container>
                <el-header>
                    <div class="left-panel">
                        <el-button type="primary" @click="back">确定</el-button>
                        <el-button type="primary" icon="el-icon-plus" v-auth="'goods.add'" @click="add">新增</el-button>
                    </div>
                    <div class="right-panel">
                        <div class="right-panel-search">
						    <el-input v-model="search.goods_name" placeholder="产品型号" clearable @keyup.enter="upsearch"></el-input>
                            <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                            <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="nopadding">
                    <scTable ref="table" row-key="id" :apiObj="apiObj" @select-all="selectAll" @selection-change="selectionChange" stripe remoteSort remoteFilter border height="650" :params="query">
                        <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                        <el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
                       <el-table-column label="产品编码" prop="goods_code" align="center"></el-table-column>
                        <el-table-column label="产品型号" prop="goods_name" align="center"></el-table-column>
                        <el-table-column label="产品单位" prop="unit.unit_name" align="center"></el-table-column>
                        <el-table-column label="品类" prop="cate_name" align="center"></el-table-column>
                        <el-table-column label="产品品牌" prop="brand.brand_name" align="center"></el-table-column>
                        <el-table-column label="产品图片" align="center">
                            <template #default="scope">
                                <el-image v-if="scope.row.goods_image" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.goods_image" :preview-src-list="[scope.row.goods_image]" preview-teleported="true" hide-on-click-modal align="center"></el-image>
                            </template>
                        </el-table-column>
                        <el-table-column label="采购单价" prop="cost_price" align="center"></el-table-column>
					    <el-table-column label="移动平均价" prop="average_price" align="center"></el-table-column>
                    </scTable>
                </el-main>
            </el-container>
        </el-container>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
		</template>
        <save-add v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-add>
	</el-dialog>
</template>

<script>
import saveAdd from './save.vue'
export default {
    props:{
        query: { type: Object, default: () => ({}) },
    },
    components: { saveAdd },
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '选择商品',
				edit: '选择商品',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			showGrouploading: false,
			groupFilterText: '',
			group: [],
            groupProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			apiObj: this.$API.goods.goods.list,
			selection: [],
			search: {
				goods_name: null,
                goods_code:null,
                goods_spec:null,
                goods_model:null,
                material_code:null,
			},
            dialog: {
				save: false,
			},
            type: "",
		}
	},
    watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
        this.getGroup()
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		// 表单注入数据
		setData(data) {
            this.type = data
		},
        selectAll() {
            if(this.type == 1){
                this.$message.error("只能单选！")
			    this.$refs.table.clearSelection()
            }
		},
        // 选择事件
        selectionChange(selection) {
            if(this.type == 1){
                if(selection.length > 1){
                    const del_row = selection.shift()
                    this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
                }
            }
			this.selection = selection;
		},
        // 选中返回
        back(){
            this.$emit('success', this.selection,this.type)
            this.visible = false;
        },
        //商品新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
        // 更新商品新增
        handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
        //搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
        //加载树数据
		async getGroup() {
			this.showGrouploading = true;
			var res = await this.$API.goods.goodsCate.all.get();
			this.showGrouploading = false;
			var allNode = { id: '', cate_name: '所有' }
			res.data.unshift(allNode);
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		groupClick(node) {
            const ids = this.getAncestorIds(node, this.group);
            this.pid = ids
			var params = {
				cate_id: node.id,
                ware_id: this.query.ware_id
			}
			this.$refs.table.reload(params)
        },
        getAncestorIds(node, tree) {
            const ids = [];
            this.findNodeAndAncestors(node.id, tree, ids);
            return ids;
        },
        findNodeAndAncestors(targetId, tree, ids) {
            for (const item of tree) {
                if (item.id === targetId) {
                ids.unshift(item.id);
                    return true;
                }
                if (item.children && item.children.length > 0) {
                    if (this.findNodeAndAncestors(targetId, item.children, ids)) {
                        ids.unshift(item.id);
                        return true;
                    }
                }
            }
            return false;
        },
	}
}
</script>

<style></style>
